<?php
class BugsController extends AppController {

    public $name = 'Bugs';
    public $paginate = array(
        'order' => 'Bug.created DESC'
    );

    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('index', 'add');
    }

    public function index(){
        $this->set('bugs', $this->paginate());
    }

    public function add(){
        if(!empty($this->data)){
            $this->Bug->create();
            if($this->Bug->save($this->data)){
                $this->Session->setFlash(__('The Bug has been saved', true));
            }else{
                $this->Session->setFlash(__('The Bug could not be saved. Please, try again.', true));
            }
        }else{
            $this->Session->setFlash(__('Invalid Bug', true));
        }
        $this->redirect(array('action'=>'index'));
    }

    public function delete($id=null){
        if(!$id){
            $this->Session->setFlash(__('Invalid Bug', true));
        }elseif($this->Bug->del($id)){
            $this->Session->setFlash(__('Specified Bug deleted', true));
        }else{
             $this->Session->setFlash(__('Specified Bug not deleted', true));
        }
        $this->redirect(array('action'=>'index'));
    }

    public function resolve($id=null){
        if(!$id){
            $this->Session->setFlash(__('Invalid Bug', true));
        }else{
            $this->Bug->id = $id;
            if($this->Bug->saveField('resolved', 1)){
                $this->Session->setFlash(__('Specified Bug revolved', true));
            }else{
                $this->Session->setFlash(__('Specified Bug not resolved', true));
            }
        }
        $this->redirect(array('action'=>'index'));
    }

}
?>